package top.hkyzf.study.leetcode.array;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-13 16:17
 */
public class 至少是其他数字两倍的最大数_747 {
    // 两次循环方式
    public int dominantIndex1(int[] nums) {
        int max = 0;
        int index = 0;
        for (int i=0; i<nums.length; i++) {
            if(nums[i] > max) {
                max = nums[i];
                index = i;
            }
        }
        int temp = max / 2;
        for (int i=0; i<nums.length; i++) {
            if(nums[i] > temp && i != index) {
                return -1;
            }
        }
        return index;
    }
    // 一次循环找出最大和次大元素
    public int dominantIndex(int[] nums) {
        int max = 0;
        int preMax = 0;
        int index = 0;
        for (int i=0; i<nums.length; i++) {
            if(nums[i] > max) {
                preMax = max;
                max = nums[i];
                index = i;
            } else {
                if(nums[i] > preMax) {
                    preMax = nums[i];
                }
            }
        }
        if(preMax * 2 > max) {
            return -1;
        }
        return index;
    }

    @Test
    public void solution() {
        int[] ints = new int[]{0, 0, 3, 2};
        int maxConsecutiveOnes = dominantIndex(ints);
        System.out.println(maxConsecutiveOnes);
    }
}
